﻿/// <reference path="jasmine.js" />
/// <reference path="../../messageboard/scripts/angular.js" />
/// <reference path="../../messageboard/scripts/angular-mocks.js" />
/// <reference path="../../messageboard/js/home-index.js" />
/// <reference path="../../messageboard/scripts/angular-route.js" />

describe("home-index Tes-->", function () {

    //run a small piece of code before each test is run
    beforeEach(function () {
        module("homeIndex");
    });

    var $httpBackend; // object that can mimic backend   


    beforeEach(inject(function ($injector) { //$injector = object that can resolve these references for us

        $httpBackend = $injector.get("$httpBackend");

        $httpBackend.when("GET", "/api/topics/includeReplies=true")
            .respond([{
                id: 1,
                body: "This is a body",
                Created: "20050401",
                Title: "This is a title"
            },
            {
                id: 1,
                body: "This is a body",
                Created: "20050401",
                Title: "This is a title"
            },
            {
                id: 1,
                body: "This is a body",
                Created: "20050401",
                Title: "This is a title"
            }]);
    }));

    //called every time after test is issued
    afterEach(function () {
        $httpBackend.verifyNoOutstandingExpectation();
        $httpBackend.verifyNoOutstandingRequest();
    });

    describe("dataService->", function () {
        it("can load topic", inject(function (dataService) {
            expect(dataService.topics).toEqual([]);
            $httpBackend.expectGET("/api/topics/includeReplies=true");
            dataService.getTopics();
            $httpBackend.flush();
            expect(dataService.topics.length).toBeGreaterThan(0);
            expect(dataService.topics.length).toEqual(3);
        }));
    });


    describe("topicController ->", function () {
        it("load data", inject(function ($controller, $http, dataService) {

            var theScope = {};
            $httpBackend.expectGET("/api/topics/includeReplies=true");

            var ctrl = $controller("topicsController", {
                $scope: theScope,
                $http: $http,
                dataService : dataService
            });
            
            $httpBackend.flush();

            expect(ctrl).not.toBeNull();
            expect(theScope.data).toBeDefined();
        }));

    });

});